package com.lsw.jgyf.ui.activity;

import android.content.Intent;
import android.text.TextUtils;

import com.lsw.dialog.DialogProgress;
import com.lsw.jgyf.httpapi.HttpApi;
import com.lsw.jgyf.inject.Constants;
import com.lsw.jgyf.ui.base.presenter.BasePresenter;
import com.lsw.shareUser.ShareUserInfoUtil;
import com.lsw.util.LogUtils;
import com.lsw.util.StringUtil;
import com.lsw.util.ToastUtil;

import javax.inject.Inject;

import io.reactivex.disposables.Disposable;

/**
 * Created by Administrator on 2018/7/10 0010.
 */

public class ChangePassWordPresenter extends BasePresenter<ChangePassWordContract.View> implements ChangePassWordContract.Presenter {

    @Inject
    HttpApi httpApi;

    @Inject
    public ChangePassWordPresenter() {

    }

    @Override
    public void ChangePhone(String old_password, String password, String password_confirmation) {
        if (StringUtil.isNullOrEmpty(old_password)) {
            ToastUtil.showToast(mContext, "请输入原密码！");
            return;
        }
        if (StringUtil.isNullOrEmpty(password)) {
            ToastUtil.showToast(mContext, "请输入新密码！");
            return;
        }
        if (StringUtil.isNullOrEmpty(password_confirmation)) {
            ToastUtil.showToast(mContext, "请再次输入新密码！");
            return;
        }
        if (!TextUtils.equals(password, password_confirmation)) {
            ToastUtil.showToast(mContext, "两次密码不一致！");
            return;
        }
        DialogProgress progress = new DialogProgress(mContext);
        progress.show();
        Disposable disposable = httpApi
                .ChangePassWord(old_password, password, password_confirmation)
                .subscribe(baseBean -> {
                    progress.dismiss();
                    if (baseBean.getStatus() == Constants.CODE_SUCESS) {
                        ShareUserInfoUtil.getInstance(mContext).setString(ShareUserInfoUtil.ID, baseBean.getData().getId() + "");
                        ShareUserInfoUtil.getInstance(mContext).setString(ShareUserInfoUtil.USERNAME, baseBean.getData().getUsername());
                        ShareUserInfoUtil.getInstance(mContext).setString(ShareUserInfoUtil.MOBILE, baseBean.getData().getMobile());
                        ShareUserInfoUtil.getInstance(mContext).setString(ShareUserInfoUtil.STORE_ID, baseBean.getData().getStore_id() + "");
                        ShareUserInfoUtil.getInstance(mContext).setString(ShareUserInfoUtil.IMG_ID, baseBean.getData().getImg_id() + "");
                        ShareUserInfoUtil.getInstance(mContext).setString(ShareUserInfoUtil.TYPE, baseBean.getData().getType() + "");
                        ShareUserInfoUtil.getInstance(mContext).setString(ShareUserInfoUtil.STATUS, baseBean.getData().getStatus() + "");
                        ShareUserInfoUtil.getInstance(mContext).setString(ShareUserInfoUtil.CREATE_TIME, baseBean.getData().getCreate_time());
                        ShareUserInfoUtil.getInstance(mContext).setString(ShareUserInfoUtil.UPDATE_TIME, baseBean.getData().getUpdate_time());
                        mView.ChangeSucceed();
                    } else if (baseBean.getStatus() == Constants.CODE_LOGIN) {
                        ToastUtil.showToast(mContext, "您的登录信息已过期，请重新登录！");
                        mView.openlogin();
                    } else {
                        ToastUtil.showToast(mContext, baseBean.getMessage().toString());
                    }
                }, throwable -> {
                    progress.dismiss();
                    LogUtils.e("TAG_Enroll", throwable.getMessage());
                });
        mCompositeDisposable.add(disposable);

    }
}
